using System.Linq;
using Shop.Backend.Api.Entity;
using Shop.Backend.Api.Params;
using Shop.Backend.Api.Repository;
using Shop.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Shop.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //这里的控制器是 shops
    public class ShopsController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private IRepository<Goods> _goodsRepository;
        private IRepository<Shops> _shopsRepository;
        private IRepository<Suppliers> _suppliersRepository;

        private TokenParameter _tokenParameter;


        public ShopsController(IConfiguration configuration
        , IRepository<Users> usersRepository
        , IRepository<Goods> goodsRepository
        , IRepository<Shops> shopsRepository
        , IRepository<Suppliers> suppliersRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _goodsRepository = goodsRepository;
            _shopsRepository = shopsRepository;
            _suppliersRepository = suppliersRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        [HttpGet]
        public dynamic Get([FromQuery] QueryWithPager query)
        {
            // get请求默认从url中获取参数，如果需要使用实体接收参数，需要FromQuery特性
            var pageIndex = query.Pager.PageIndex;
            var pageSize = query.Pager.PageSize;

            var keyword = string.IsNullOrEmpty(query.Keyword) ? "" : query.Keyword.Trim();

            var shops = _shopsRepository.Table.Where(x => x.IsDeleted == false).ToList().Select(a =>
            {
                var supplier = _suppliersRepository.Table.ToList().Where(x => x.Id == a.SuppliersId);
                return new
                {
                    Id = a.Id,
                    ShopName = a.ShopName,
                    ShopAddress = a.ShopAddress,
                    ShopPhone = a.ShopPhone,
                    SuppliersName = supplier.FirstOrDefault().SuppliersName,
                    CreatedTime = a.CreatedTime,
                    IsActived = a.IsActived
                };
            });

            // 如果keyword不为空，则再去查询用户名
            if (!string.IsNullOrEmpty(keyword))
            {
                shops = shops.Where(x => x.Id.ToString() == keyword
              || x.ShopName.Contains(keyword)
              || x.ShopAddress.Contains(keyword)); //未完善的模糊查询
            }
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = new
                {
                    Data = shops,
                    Pager = new
                    {
                        pageIndex,
                        pageSize,
                        rowsTotal = shops.Count()
                    }
                },
                Msg = "获取店铺成功^_^"
            });
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var goods = _goodsRepository.GetById(id);
            return new
            {
                Code = 1000,
                Data = goods,
                Msg = "获取指定店铺成功^_^"
            };
        }

        //修改商家状态的方法
        [HttpPut, Route("status")]
        public dynamic PutStatus(CreateShops updateShop)
        {
            var id = updateShop.id;
            var user = _shopsRepository.GetById(id);


            if (user == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要更新的商店不存在，请确认后重试=_="
                };
            }

            user.IsActived = updateShop.IsActived;
            _shopsRepository.Update(user);

            return new
            {
                Code = 1000,
                Data = user,
                Msg = "更新商家状态成功"
            };
        }



        [HttpPost]
        public dynamic Post(CreateShops createShops)
        {
            var goodsName = createShops.ShopName.Trim();
            var shopAddress = createShops.ShopAddress.Trim();
            var shopPhone = createShops.ShopPhone.Trim();

            if (string.IsNullOrEmpty(goodsName))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "店铺名不能为空"
                };
            }
            if (string.IsNullOrEmpty(shopAddress))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "地址不能为空"
                };
            }
            if (string.IsNullOrEmpty(shopPhone))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "联系方式不能为空"
                };
            }

            //查查供应商存在不咯
            var supplier = _suppliersRepository.Table.ToList().Where(x => x.SuppliersName == createShops.SuppliersName && x.IsDeleted == false);
            if (supplier == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "该供应商不存在请检查后重试"
                };
            }
            var shops = new Shops
            {
                ShopName = createShops.ShopName,
                SuppliersId = supplier.FirstOrDefault().Id,
                // ShopState = createShops.ShopState,
                ShopAddress = createShops.ShopAddress,
                ShopPhone = createShops.ShopPhone
            };

            _shopsRepository.Insert(shops);
            return new
            {
                Code = 1000,
                Data = shops,
                Msg = "添加店铺成功^_^"
            };
        }
        //编辑指定店铺
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateShops updateShops)//未完善
        {
            var shopName = updateShops.ShopName.Trim();
            var shopAddress = updateShops.ShopAddress.Trim();
            var shopPhone = updateShops.ShopPhone.Trim();
            // var shopState = updateShops.ShopState;

            if (string.IsNullOrEmpty(shopName)
            && string.IsNullOrEmpty(shopAddress)
            && string.IsNullOrEmpty(shopPhone))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "商铺名或商铺地址、商铺联系方式不能为空,编辑店铺失败i.i"

                };
            }

            var shops = _shopsRepository.GetById(id);

            if (shops == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "编辑的商铺不存在。。。i.i"

                };
            }

            shops.ShopName = updateShops.ShopName;
            shops.ShopAddress = updateShops.ShopAddress;
            shops.ShopPhone = updateShops.ShopPhone;
            // shops.ShopState = updateShops.ShopState;

            //查查供应商存在不咯
            var supplier = _suppliersRepository.Table.ToList().Where(x => x.SuppliersName == updateShops.SuppliersName && x.IsDeleted == false);
            if (supplier == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "该供应商不存在请检查后重试"
                };
            }
            shops.SuppliersId = supplier.FirstOrDefault().Id;


            _shopsRepository.Update(shops);

            return new
            {
                Code = 1000,
                Data = shops,
                Msg = "编辑店铺成功^_^"
            };

        }

        //封禁或删除店铺

        [HttpDelete("{id}")]

        public dynamic Delete(int id)
        {
            var shop = _shopsRepository.GetById(id);

            if (shop == null)
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "要删除店铺不存在-.-"
                };

            }

            shop.IsActived = false;
            shop.IsDeleted = true;

            _shopsRepository.Update(shop);


            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除店铺成功^_^"
            };
        }


    }
}